package com.cherrypt.api.core.controller;

import com.cherrypt.api.common.constant.WebConst;
import com.cherrypt.api.common.exception.TipException;
import com.cherrypt.api.core.service.*;
import com.cherrypt.api.model.bo.ConfigBo;
import com.cherrypt.api.model.bo.NoticeBo;
import com.cherrypt.api.model.dto.ConfigDto;
import com.cherrypt.api.model.dto.NoticeDto;
import com.cherrypt.api.model.dto.UserConfigDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Api(tags = "获取配置、更新配置、获取公告")
@RestController
@RequestMapping("${api.url.config.value}")
public class ConfigController {
    @Value("${api.config.notice.len}")
    private int noticeLen;

    @Autowired
    private ConfigService configService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private LevelService levelService;

    /**
     * 获取配置参数
     *
     * @return {@link ConfigDto}
     */
    @ApiOperation(value = "获取配置参数")
    @GetMapping(value = "")
    public ResponseEntity<ConfigDto> getConfig() throws TipException {
        return ResponseEntity.ok(ConfigBo.getConfigDto(configService.getConfig()));
    }

    /**
     * 已登录的用户获取配置
     *
     * @return {@link UserConfigDto}
     */
    @ApiOperation(value = "已登录的用户获取配置")
    @ApiImplicitParam(name = "token", value = "Header携带token辨识用户", example = WebConst.TOKEN,
            dataType = "string", paramType = "header", required = true)
    @GetMapping(value = "${api.url.config.user}")
    public ResponseEntity<UserConfigDto> getConfigByUser() throws TipException, IOException {
        return ResponseEntity.ok(
                ConfigBo.getUserConfigDto(
                        configService.getConfig(),
                        categoryService.getCategoryList(),
                        levelService.getLevelList()));
    }

    /**
     * 已登录用户获取公告
     *
     * @return {@link java.util.List<NoticeDto>}
     */
    @ApiOperation(value = "已登录用户获取公告")
    @ApiImplicitParam(name = "token", value = "Header携带token辨识用户", example = WebConst.TOKEN,
            dataType = "string", paramType = "header", required = true)
    @GetMapping(value = "${api.url.config.notice}")
    public ResponseEntity<List<NoticeDto>> getNotice() {
        return ResponseEntity.ok(NoticeBo.getNoticeDtoList(noticeService.getNowNotice(noticeLen)));
    }

    // TODO 管理员更新配置
}
